<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Use mock instead of class for new objects</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.uopz-set-mock" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">uopz_set_mock</h1>
  <p class="verinfo">(PECL uopz 5)</p><p class="refpurpose"><span class="refname">uopz_set_mock</span> &mdash; <span class="dc-title">Use mock instead of class for new objects</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.uopz-set-mock-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>uopz_set_mock</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$class</code></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$mock</code></span>
   ) : <span class="type"><span class="type void">void</span></span></div>

  <p class="para rdfs-comment">
   If <code class="parameter">mock</code> is a string containing the name of a class then it will be instantiated instead of
   <code class="parameter">class</code>. <code class="parameter">mock</code> can also be an object.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Only dynamic access to properties and methods will use the <code class="parameter">mock</code> object.
    Static access still uses the original <code class="parameter">class</code>.
    See <a href="uopz-set-mock.html#uopz_set_mock.example.static" class="link">example</a> below.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.uopz-set-mock-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>
<code class="parameter">class</code></dt>

    <dd>

     <p class="para">
      The name of the class to be mocked.
     </p>
    </dd>

   
   
    <dt>
<code class="parameter">mock</code></dt>

    <dd>

     <p class="para">
      The mock to use in the form of a string containing the name of the class to use or an object.
      If a string is passed, it has to be the fully qualified class name. It is
      recommended to use the <code class="code">::class</code> magic constant in this case.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 changelog" id="refsect1-function.uopz-set-mock-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>uopz 6.0.0</td>
      <td>
       Mocking static members is no longer supported by this function.
       <span class="function"><a href="uopz_redefine.html" class="function">uopz_redefine()</a></span> and <span class="function"><a href="uopz_set_return.html" class="function">uopz_set_return()</a></span>,
       or <a href="book.componere.html" class="link">Componere</a> can be used instead.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.uopz-set-mock-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="uopz-get-exit-status.example.basic">
   <p><strong>Example #1 <span class="function"><strong>uopz_set_mock()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">who</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"A"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">mockA&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">who</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"mockA"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">uopz_set_mock</span><span style="color: #007700">(</span><span style="color: #0000BB">A</span><span style="color: #007700">::class,&nbsp;</span><span style="color: #0000BB">mockA</span><span style="color: #007700">::class);<br />(new&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">who</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
mockA
</pre></div>
   </div>
  </div>
  <div class="example" id="uopz-get-hook.example.basic">
   <p><strong>Example #2 <span class="function"><strong>uopz_set_mock()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">who</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"A"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">uopz_set_mock</span><span style="color: #007700">(</span><span style="color: #0000BB">A</span><span style="color: #007700">::class,&nbsp;new&nbsp;class&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">who</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"mockA"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});<br />(new&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">)-&gt;</span><span style="color: #0000BB">who</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
mockA
</pre></div>
   </div>
  </div>
  <div class="example" id="example-588">
   <p><strong>Example #3 <span class="function"><strong>uopz_set_mock()</strong></span> and static members</strong></p>
   <div class="example-contents"><p>
    As of uopz 6.0.0 mocking static members is no longer supported.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;</span><span style="color: #0000BB">CON&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'A'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;function&nbsp;</span><span style="color: #0000BB">who</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"A"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">uopz_set_mock</span><span style="color: #007700">(</span><span style="color: #0000BB">A</span><span style="color: #007700">::class,&nbsp;new&nbsp;class&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;</span><span style="color: #0000BB">CON&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'mockA'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;function&nbsp;</span><span style="color: #0000BB">who</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"mockA"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});<br />echo&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">::</span><span style="color: #0000BB">CON</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">A</span><span style="color: #007700">::</span><span style="color: #0000BB">who</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
A
A
</pre></div>
   </div>
    <div class="example-contents"><p>
     Output of the above example in uopz 5:
    </p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
mockA
mockA
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.uopz-set-mock-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="uopz_get_mock.html" class="function" rel="rdfs-seeAlso">uopz_get_mock()</a> - Get the current mock for a class</span></li>
   <li class="member"><span class="function"><a href="uopz_unset_mock.html" class="function" rel="rdfs-seeAlso">uopz_unset_mock()</a> - Unset previously set mock</span></li>
  </ul>
 </div>


</div></div></div></body></html>